<template>
  <view class="collage-page">
    <!-- 顶部分类Tab -->
    <view class="top-wrapper">
      <scroll-view class="category-tabs" scroll-x scroll-with-animation show-scrollbar="{{false}}">
        <view class="tabs-wrapper">
          <view 
            v-for="(cat, i) in categories" 
            :key="i"
            class="tab-item"
            :class="{ active: currentCategory === cat.value }"
            @click="switchCategory(cat.value)"
          >
            <text class="tab-emoji">{{ cat.emoji }}</text>
            <text class="tab-text">{{ cat.label }}</text>
          </view>
        </view>
      </scroll-view>
    </view>

    <!-- 瀑布流列表 -->
    <view class="waterfall-container" v-if="filteredCollages.length > 0">
      <view class="waterfall-column">
        <view 
          v-for="(item, i) in leftColumn" 
          :key="i"
          class="collage-card"
          @click="viewDetail(item)"
        >
          <!-- 喜欢按钮 -->
          <view class="like-btn" @click.stop="toggleLike(item)">
            <text class="like-icon">{{ item.liked ? '❤️' : '🤍' }}</text>
          </view>
          
          <image :src="item.coverImage" class="card-image" mode="widthFix" />
          <view class="card-info">
            <view class="tags">
              <view v-if="item.occasion" class="tag occasion-tag">
                <text class="tag-text">{{ item.occasion }}</text>
              </view>
              <view v-if="item.season" class="tag season-tag">
                <text class="tag-text">{{ item.season }}</text>
              </view>
            </view>
            <text class="date">{{ formatDate(item.createTime) }}</text>
          </view>
        </view>
      </view>
      
      <view class="waterfall-column">
        <view 
          v-for="(item, i) in rightColumn" 
          :key="i"
          class="collage-card"
          @click="viewDetail(item)"
        >
          <!-- 喜欢按钮 -->
          <view class="like-btn" @click.stop="toggleLike(item)">
            <text class="like-icon">{{ item.liked ? '❤️' : '🤍' }}</text>
          </view>
          
          <image :src="item.coverImage" class="card-image" mode="widthFix" />
          <view class="card-info">
            <view class="tags">
              <view v-if="item.occasion" class="tag occasion-tag">
                <text class="tag-text">{{ item.occasion }}</text>
              </view>
              <view v-if="item.season" class="tag season-tag">
                <text class="tag-text">{{ item.season }}</text>
              </view>
            </view>
            <text class="date">{{ formatDate(item.createTime) }}</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 空状态 -->
    <view v-else class="empty-state">
      <view class="empty-content">
        <text class="empty-icon">✨</text>
        <text class="empty-title">还没有搭配哦~</text>
        <text class="empty-desc">记录你的每一套美美的穿搭</text>
        <view class="add-button" @click="addCollage">
          <text class="add-icon">+</text>
          <text class="add-text">创建搭配</text>
        </view>
      </view>
    </view>

    <!-- 悬浮添加按钮 -->
    <view v-if="filteredCollages.length > 0" class="fab" @click="addCollage">
      <text class="fab-icon">+</text>
      <view class="fab-ripple"></view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue'
import { onShow } from '@dcloudio/uni-app'

// 分类列表（添加可爱的emoji）
const categories = [
  { label: '全部', value: 'all', emoji: '✨' },
  { label: '工作', value: 'work', emoji: '💼' },
  { label: '运动', value: 'sport', emoji: '🏃‍♀️' },
  { label: '休闲', value: 'casual', emoji: '☕' },
  { label: '约会', value: 'date', emoji: '💕' },
  { label: '度假', value: 'vacation', emoji: '🌴' }
]

const currentCategory = ref('all')

// 静态演示数据
const collages = ref([
  {
    id: 1,
    occasion: '工作',
    season: '夏',
    coverImage: 'https://picsum.photos/300/400?random=1',
    createTime: Date.now() - 86400000
  },
  {
    id: 2,
    occasion: '休闲',
    season: '春',
    coverImage: 'https://picsum.photos/300/500?random=2',
    createTime: Date.now() - 172800000
  },
  {
    id: 3,
    occasion: '约会',
    season: '秋',
    coverImage: 'https://picsum.photos/300/450?random=3',
    createTime: Date.now() - 259200000
  },
  {
    id: 4,
    occasion: '运动',
    season: '夏',
    coverImage: 'https://picsum.photos/300/480?random=4',
    createTime: Date.now() - 345600000
  },
  {
    id: 5,
    occasion: '度假',
    season: '所有季节',
    coverImage: 'https://picsum.photos/300/420?random=5',
    createTime: Date.now() - 432000000
  }
])

// 过滤后的搭配列表
const filteredCollages = computed(() => {
  if (currentCategory.value === 'all') {
    return collages.value
  }
  return collages.value.filter(item => item.occasion === currentCategory.value)
})

// 瀑布流左列
const leftColumn = computed(() => {
  return filteredCollages.value.filter((_, index) => index % 2 === 0)
})

// 瀑布流右列
const rightColumn = computed(() => {
  return filteredCollages.value.filter((_, index) => index % 2 === 1)
})

// 切换分类
function switchCategory(value) {
  currentCategory.value = value
}

// 添加搭配
function addCollage() {
  uni.navigateTo({
    url: '/pages/collage/edit'
  })
}

// 查看详情
function viewDetail(item) {
  uni.navigateTo({
    url: '/pages/collage/edit?id=' + item.id
  })
}

// 切换喜欢状态
function toggleLike(item) {
  item.liked = !item.liked
  // 保存到本地存储
  uni.setStorageSync('collages', collages.value)
  
  // 触觉反馈
  uni.vibrateShort({ type: 'light' })
}

// 格式化日期
function formatDate(timestamp) {
  const date = new Date(timestamp)
  const month = date.getMonth() + 1
  const day = date.getDate()
  return `${month}月${day}日`
}

// 页面显示时加载数据
onShow(() => {
  loadCollages()
})

function loadCollages() {
  const data = uni.getStorageSync('collages') || []
  // 如果有保存的数据，使用保存的数据；否则使用静态演示数据
  if (data.length > 0) {
    collages.value = data
  }
  // 保持静态演示数据用于展示效果
}
</script>

<style lang="scss" scoped>
.collage-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #FFF5F7 0%, #F3E8FF 100%);
  padding-bottom: calc(140rpx + env(safe-area-inset-bottom)); // 为悬浮TabBar留出空间
}

.top-wrapper {
  position: sticky;
  top: 0;
  z-index: 99;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20rpx);
  box-shadow: 0 4rpx 20rpx rgba(255, 182, 193, 0.1);
}

.category-tabs {
  white-space: nowrap;
}

.tabs-wrapper {
  display: inline-flex;
  padding: 20rpx 24rpx;
  gap: 12rpx;
}

.tab-item {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16rpx 24rpx;
  border-radius: 28rpx;
  background: transparent;
  transition: all 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  
  .tab-emoji {
    font-size: 36rpx;
    margin-bottom: 6rpx;
    transition: transform 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  }
  
  .tab-text {
    font-size: 24rpx;
    color: #8B7B8B;
    font-weight: 500;
    transition: all 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  }
  
  &.active {
    background: linear-gradient(135deg, #FFE5EC 0%, #F3E8FF 100%);
    box-shadow: 0 4rpx 16rpx rgba(255, 182, 193, 0.25);
    
    .tab-emoji {
      transform: scale(1.1);
    }
    
    .tab-text {
      color: #D87093;
      font-weight: 600;
    }
  }
  
  &:active {
    transform: scale(0.95);
  }
}

.waterfall-container {
  display: flex;
  gap: 20rpx;
  padding: 24rpx;
}

.waterfall-column {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 20rpx;
}

.collage-card {
  background: #fff;
  border-radius: 24rpx;
  overflow: hidden;
  box-shadow: 0 8rpx 32rpx rgba(255, 182, 193, 0.15);
  transition: all 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  
  &:active {
    transform: scale(0.98);
    box-shadow: 0 4rpx 16rpx rgba(255, 182, 193, 0.2);
  }
}

.like-btn {
  position: absolute;
  top: 16rpx;
  right: 16rpx;
  width: 56rpx;
  height: 56rpx;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10rpx);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
  z-index: 10;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  
  .like-icon {
    font-size: 32rpx;
    line-height: 1;
  }
  
  &:active {
    transform: scale(1.2);
  }
}

.card-image {
  width: 100%;
  display: block;
  border-radius: 24rpx 24rpx 0 0;
}

.card-info {
  padding: 20rpx;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 12rpx;
  margin-bottom: 16rpx;
}

.tag {
  display: inline-flex;
  align-items: center;
  padding: 8rpx 16rpx;
  border-radius: 20rpx;
  
  .tag-text {
    font-size: 22rpx;
    font-weight: 500;
    line-height: 1;
  }
}

.occasion-tag {
  background: linear-gradient(135deg, #FFE5EC 0%, #FFD4E5 100%);
  
  .tag-text {
    color: #D87093;
  }
}

.season-tag {
  background: linear-gradient(135deg, #F3E8FF 0%, #E6D4FF 100%);
  
  .tag-text {
    color: #9370DB;
  }
}

.date {
  font-size: 24rpx;
  color: #B8A8B8;
  font-weight: 500;
}

.empty-state {
  min-height: 60vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60rpx;
}

.empty-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.empty-icon {
  font-size: 160rpx;
  margin-bottom: 32rpx;
  animation: float 3s ease-in-out infinite;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-20rpx);
  }
}

.empty-title {
  font-size: 32rpx;
  color: #8B7B8B;
  font-weight: 600;
  margin-bottom: 16rpx;
}

.empty-desc {
  font-size: 26rpx;
  color: #B8A8B8;
  margin-bottom: 48rpx;
  line-height: 1.6;
}

.add-button {
  display: flex;
  align-items: center;
  gap: 12rpx;
  padding: 24rpx 56rpx;
  background: linear-gradient(135deg, #FFB6C1 0%, #DDA0DD 100%);
  color: #fff;
  border-radius: 48rpx;
  font-size: 30rpx;
  font-weight: 600;
  box-shadow: 0 8rpx 24rpx rgba(255, 182, 193, 0.4);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  
  .add-icon {
    font-size: 36rpx;
    font-weight: 300;
  }
  
  .add-text {
    line-height: 1;
  }
  
  &:active {
    transform: scale(0.95);
    box-shadow: 0 4rpx 16rpx rgba(255, 182, 193, 0.3);
  }
}

.fab {
  position: fixed;
  right: 40rpx;
  bottom: 140rpx;
  width: 112rpx;
  height: 112rpx;
  border-radius: 50%;
  background: linear-gradient(135deg, #FFB6C1 0%, #DDA0DD 100%);
  box-shadow: 0 8rpx 32rpx rgba(255, 182, 193, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 100;
  
  .fab-icon {
    font-size: 56rpx;
    color: #fff;
    font-weight: 300;
    line-height: 1;
  }
  
  .fab-ripple {
    position: absolute;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    border: 3rpx solid rgba(255, 182, 193, 0.6);
    animation: ripple 2s infinite;
  }
  
  &:active {
    transform: scale(0.9);
  }
}

@keyframes ripple {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  100% {
    transform: scale(1.5);
    opacity: 0;
  }
}
</style>


